package com.fram.service.admin.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.fram.bean.admin.volume.request.VolumePaperRquest;
import com.fram.bean.admin.volume.request.VolumeUpdateRequest;
import com.fram.bean.admin.volume.response.PaperResponse;
import com.fram.bean.admin.volume.response.UserAnswerResponse;
import com.fram.bean.admin.volume.response.VolumeListResponse;
import com.fram.bean.api.volume.response.VolumeResponse;
import com.fram.bean.sys.PageBean;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.exception.ProValiDataException;
import com.fram.entity.VolumeAnswer;
import com.fram.entity.VolumeOption;
import com.fram.entity.VolumePaper;
import com.fram.entity.VolumeQuestion;
import com.fram.mapper.VolumeAnswerMapper;
import com.fram.mapper.VolumeOptionMapper;
import com.fram.mapper.VolumePaperMapper;
import com.fram.mapper.VolumeQuestionMapper;
import com.fram.service.admin.VolumeAddService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class VolumeAddServiceImpl implements VolumeAddService {
	@Autowired
	private VolumeQuestionMapper volumeQuestionMapper;
	@Autowired
	private VolumePaperMapper volumePaperMapper;
	@Autowired
	private VolumeOptionMapper volumeOptionMapper;
	@Autowired
	private VolumeAnswerMapper volumeAnswerMapper;

	/** 添加问题 **/
	@Override
	public void addQuestion(VolumeListResponse request) throws ProException {
		try {
			if (volumeQuestionMapper.insertSelective(request) == 0) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
			}
			List<VolumeOption> option= request.getOption();
			for (VolumeOption volumeOption : option) {
				Long questionId = volumeQuestionMapper.selectByPrimary(request.getDirection());
				volumeOption.setQuestionId(questionId);
				if(volumeOptionMapper.insertSelective(volumeOption) ==0) {
					throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
				}
			}
		}catch (DataAccessException e) {
			throw new ProValiDataException(ErrCode.ERR_HAVE_EXISTED.getReturnCode(), ErrCode.ERR_HAVE_EXISTED.getReturnMsg());
		}
	}

	/** 试卷添加问题 **/
	@Override
	public void addPaper(VolumePaperRquest request) throws ProException {
		for (Long Question : request.getQuestionId()) {
			VolumePaper paper=new VolumePaper();
			paper.setQuestionId(Question);
			paper.setVersion(request.getVersion());
			if (volumePaperMapper.insertSelective(paper)==0) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
			}
		}
	}

	/** 一个问题增加多个选择题（abcde） **/
	@Override
	public void addOption(List<VolumeOption> request) throws ProException {
		for (VolumeOption volumeOption : request) {
			if(volumeOptionMapper.insertSelective(volumeOption) == 0) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
			}
		}
	}

	/** 后台 问题卷列表 **/
	@Override
	public PageInfo<VolumeQuestion> listTopic(PageBean<VolumeQuestion> request) throws ProException {
		/****/
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<VolumeQuestion> list = volumeQuestionMapper.listQuestion(request.getParam());
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	/** 问题详情 **/
	@Override
	public VolumeListResponse details(Long id) throws ProException {
		VolumeListResponse volumeQuestion = volumeQuestionMapper.selectQuestionById(id);
		if (volumeQuestion == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return volumeQuestion;
	}
	/**后台-删选择题**/
	@Override
	public void deleteOption(Long id) throws ProException {
		if (volumeOptionMapper.deleteByPrimaryKey(id) == 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}
//	/**后台-修改选择题**/
//	@Override
//	public void updateOption(VolumeOption request) throws ProException {
//		if (volumeOptionMapper.updateByPrimaryKeySelective(request) == 0) {
//			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
//		}
//	}
//	/**后台修改问题**/
//	@Override
//	public void updateQuestion(VolumeQuestion request) throws ProException {
//		if (volumeQuestionMapper.updateByPrimaryKeySelective(request) == 0) {
//			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
//		}
//	}

	@Override
	public List<UserAnswerResponse> listByUserId(VolumeAnswer request)throws ProException {
		List<UserAnswerResponse>  list=volumeAnswerMapper.selectByUserId(request);
		if(list == null || list.size()==0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return list;
	}
	/****/
	@Override
	public PageInfo<VolumeResponse> listPaper(PageBean<VolumePaper> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<VolumeResponse> list = volumePaperMapper.paperList(request.getParam());
		if(list==null ||list.size()==0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	@Override
	public List<VolumePaper> downPaper()throws ProException {
		if(volumePaperMapper.downPaper()==null ||volumePaperMapper.downPaper().size() ==0  ) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return volumePaperMapper.downPaper();
	}
	
	/**分页展示  根据版本，展示答完题的用户 （用户不重复）**/
	@Override
	public PageInfo<PaperResponse> listByName(PageBean<VolumeAnswer> request)throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<PaperResponse> list = volumeAnswerMapper.selectByAnswer(request.getParam());
//		List<PaperResponse> list=volumePaperMapper.selectByName(request.getParam());
		 if(list==null ||list.size()==0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		 }
		return new PageInfo<>(list);
	}

	@Override
	public void deletePaper(Long id) throws ProException {
		if(volumePaperMapper.deleteByPrimaryKey(id)==0){
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}
	/**最终  修改**/
	@Override
	public void update(VolumeUpdateRequest request) throws ProException {
 		if(request.getAnswerType()==2) {
			//如果等于2  填空题  必须将选择题 清理干净
			if(volumeOptionMapper.deleteByQuestionId(request.getId()) == 0) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
			}
			VolumeQuestion question= new VolumeQuestion();
			question.setId(request.getId());
			question.setDirection(request.getDirection());
			question.setAnswerType(request.getAnswerType());
			question.setAnswer(request.getAnswer());
			if(volumeQuestionMapper.updateByPrimaryKeySelective(question) == 0) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
			}
		}else {
			for (VolumeOption options : request.getOption()) {
				VolumeQuestion question= new VolumeQuestion();
				question.setId(request.getId());
				question.setDirection(request.getDirection());
				question.setAnswerType(request.getAnswerType());
				question.setAnswer(request.getAnswer());
				if(options.getId()==null) {
					options.setQuestionId(request.getId());
					if(volumeOptionMapper.insertSelective(options) == 0){
						throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
					}
				}else {
					VolumeOption option= new VolumeOption();
					option.setId(options.getId());
					option.setQuestionId(options.getQuestionId());
					option.setScontent(options.getScontent());
					if(volumeOptionMapper.updateByPrimaryKeySelective(option) == 0) {
						throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
					}
				}
				if(volumeQuestionMapper.updateByPrimaryKeySelective(question) == 0){
					throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
				}
			}
		}
//		/**仅修改问题*/
//		VolumeQuestion question= new VolumeQuestion();
//		question.setId(request.getId());
//		question.setDirection(request.getDirection());
//		question.setAnswerType(request.getAnswerType());
//		question.setAnswer(request.getAnswer());
//		if(volumeQuestionMapper.updateByPrimaryKeySelective(question) == 0) {
//			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
//		}
	}
}
